//
//  LLGNetworkTools.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/9.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import AFNetworking

//网络请求枚举
enum RequestType: Int {

    case GET
    case POST
}

class LLGNetworkTools: AFHTTPSessionManager {
    
    //起别名
    typealias CallBackType = (response: AnyObject?, error: NSError?) -> ()

    // MARK: --单例对象
    static let sharedTools: LLGNetworkTools = {
    
        let tools = LLGNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    // MARK: --网络请求
    func request(requestType: RequestType, url: String, params: AnyObject?, callBack: CallBackType) {
    
        if requestType == .GET {
            GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        } else {
            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        
        }
        
    }
    
    // MARK: --发送图片
    func requestImage(url: String, params: AnyObject?, imageData: NSData, name: String, callBack: CallBackType) {
    
        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            
            formData.appendPartWithFileData(imageData, name: name, fileName: "test", mimeType: "application/octet-stream")
            
            }, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
            }) { (_, error) -> Void in
                callBack(response: nil, error: error)
        }
    }
}


//发送微博接口
extension LLGNetworkTools {

    func update(accessToken: String, status: String, callBack: CallBackType) {
    
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        let params: [String: AnyObject] = [
            "access_token": accessToken,
            "status": status
        ]
        
        request(.POST, url: url, params: params, callBack: callBack)
    }
    
    func requestUpload(accessToken: String, status: String, image: UIImage, callBack: CallBackType) {
    
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let params: [String: AnyObject] = [
            "access_token": accessToken,
            "status": status
        ]
        
        //把图片转成二进制数据
//        let imageData = UIImagePNGRepresentation(image)!
        
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        
        requestImage(url, params: params, imageData: imageData, name: "pic", callBack: callBack)
        
        
    }
}

//获取首页数据
extension LLGNetworkTools {

    //获取当前登录用户及其所关注（授权）用户的最新微博
    func requestStatuses(accessToken: String, maxId: Int64, sinceId: Int64, callBack: CallBackType) {
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params: [String: AnyObject] = [
            "access_token": accessToken,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        
        let path = url + "?access_token=" + accessToken
        print(path)
        
        request(.GET, url: url, params: params, callBack: callBack)
 
    }
}

//网络登录请求方法
extension LLGNetworkTools {

    // MARK: --获取AccessToken
    func requestAccessToken(code: String, callBack: CallBackType) {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let params: [String: AnyObject] = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri":WeiboRedirect_Uri
        ]
        
        request(.POST, url: url, params: params, callBack: callBack)
        
    }
    
    // MARK: --获取用户信息
    func requestUserInfo(userAccount: LLGUserAccount, callBack: CallBackType) {
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let params: [String: AnyObject] = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        
        request(.GET, url: url, params: params, callBack: callBack)
    }
}
